import { request } from '@/utils/request'
import qs from 'qs'

//视频状态
export interface videoStatus {
  aid: number
  view: number //播放量
  danmaku: number //弹幕量
  reply: number //评论数
  favorite: number //收藏数
  coin: number //投币数
  share: number //分享数
  now_rank: number //视频当前排行
  his_rank: number //视频历史最高排行
  like: number //点赞数
  evaluation: string //视频评分
}

//视频标签
export interface videoTag {
  aid?: number //稿件avid
  bvid?: string //稿件bvid
  tag_id: number //tag_id
  tag_name: string //TAG名称
  cover?: string //TAG图片url
  head_cover?: string //TAG页面头图url
}

//游客
// interface videoVisitor {
//   access_key: string //APP登录Token
//   aid: number //稿件avid
//   cid: number //视频cid
//   csrf: string //CSRF Token（位于cookie）
// }

export interface ownerInfo {
  mid: number //up主mid
  name: string //up主昵称
  face: string //头像图url
}
//视频详细信息
export interface videoInfo {
  aid?: number //稿件avid
  bvid?: string //稿件bvid
  cid: number
  tid: number
  pic: string //稿件封面图片url
  title: string //稿件标题
  pubdate: number //稿件发布时间
  ctime: number //用户投稿时间
  desc: string //视频简介
  redirect_url: string //重定向url
  owner: ownerInfo
  stat: videoStatus
}

//获取视频详细信息
export const reqVideoInfo = (id: number | string) =>
  request<videoInfo>({
    url: '/web-interface/view',
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//获取视频简介
export const reqVideoDesc = (id: number | string) =>
  request<string>({
    url: '/web-interface/archive/desc',
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//* -----------------------点赞---------------------------
//点赞视频 1点赞2取消
//还有一个必传字段csrf(cookie中的数据)，这里就不传了，在中转服务器中会加上

export const reqVideoLike = (id: number | string, like: 1 | 2) =>
  request<null>({
    url: '/web-interface/archive/like',
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
    data: qs.stringify({
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
      like,
    }),
  })

//1是已点赞，0是未点赞
export const reqHasLike = (id: number | string) =>
  request<1 | 0>({
    url: '/web-interface/archive/has/like',
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//* -----------------------投币--------------------------
/* 
code返回
0：成功
-101：账号未登录
-102：账号被封停
-104：硬币不足
-111：csrf校验失败
-400：请求错误
10003：不存在该稿件
34002：不能给自己投币
34003：非法的投币数量
34004：投币间隔太短
34005：超过投币上限 
*/
//还有一个必传字段csrf(cookie中的数据)，这里就不传了，在中转服务器中会加上
export const reqVideoCoin = (id: number | string, multiply: 1 | 2) =>
  request<null>({
    url: `/web-interface/coin/add`,
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
    data: qs.stringify({
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
      multiply,
    }),
  })

export const reqHasCoin = (id: number | string) =>
  request<{ multiply: 0 | 1 | 2 }>({
    url: '/web-interface/archive/coins',
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//* ----------------判断视频是否被收藏----------------
//!这里特殊，无论是aid还是bvid，都是使用aid字段发送
export const reqHasSub = (aid: number | string) =>
  request<{ favoured: boolean }>({
    url: '/web-interface/archive/coins',
    params: {
      aid,
    },
  })

//* ----------------获取视频在线人数----------------
export interface onlineNumber {
  total: string //所有终端总计人数
  count: string //web端实时在线人数
}
//视频在线人数
export const reqVideoOnline = (id: string | number, cid: number) =>
  request<onlineNumber>({
    url: `/player/online/total`,
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
      cid,
    },
  })

export interface videoRecommend {
  aid: number
  bvid: string
  cid: number
  pic: string
  desc: string
  title: string
  pubdate: number
  ctime: number
  owner: {
    mid: number
    name: string
  }
  stat: {
    aid: number
    danmuku: number
    view: number
  }
}

//视频推荐
export const reqVideoRecommend = (id: string | number) =>
  request<videoRecommend[]>({
    url: `/web-interface/archive/related`,
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//视频状态数
export const reqVideoStat = (id: string | number) =>
  request<videoStatus>({
    url: `/web-interface/archive/stat`,
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//视频标签
export const reqVideoTag = (id: string | number) =>
  request<videoTag[]>({
    url: `/tag/archive/tags`,
    params: {
      [typeof id === 'number' ? 'aid' : 'bvid']: id,
    },
  })

//视频url 当传入的id是number的时候，参数名是aid，当是string时，参数名是bid
export const reqVideoUrl = (id: number | string, cid: number) =>
  request<VideoUrl>({
    url: `/player/playurl`,
    params: {
      [typeof id === 'number' ? 'avid' : 'bvid']: id,
      cid,
    },
  })

interface VideoUrl {
  quality: number // 分辨率代码
  format: string //视频格式
  timelength: number //视频长度
  accept_format: string // 'flv_p60,flv,flv720,flv480,mp4'
  accept_description: string[] //['高清 1080P60', '高清 1080P', '高清 720P', '清晰 480P', '流畅 360P']
  accept_quality: number[] //[116, 80, 64, 32, 16]
  durl: {
    order: 1
    length: 134051
    size: 34629280
    url: string
  }[]

  support_formats: {
    quality: 116
    format: 'flv_p60'
    new_description: '1080P 60帧'
    display_desc: '1080P'
    superscript: '60帧'
  }[]

  high_format: null
}

export interface memberList {
  mid: number
  count: number
  rcount: number
  ctime: number
  like: number
  hate: number
  member: {
    mid: number
    uname: string
    avatar: string
  }
  content: {
    message: string
  }
  replies: [
    {
      content: {
        message: string
      }
      ctime: number
      like: number
      hate: number
      member: {
        mid: number
        uname: string
        avatar: string
        sign: string
      }
    },
  ]
}

export interface commonPage {
  num: number
  size: number
  acount: number
}
export interface controlTool {
  root_input_text: string
  giveup_input_text: string
  answer_guide_text: string
  answer_guide_android_url: string
}
export interface commonList {
  data: {
    page: commonPage
    control: controlTool
    replies: memberList[]
  }
}

//根据id查询评论区，根据type划分不同板块的类型，目前只做视频播放页，所以type写死1
//sort排序方式，0时间，1点赞，2回复量。nohot:1为不显示，0为显示，默认为0。ps每页条数默认20
export const reqVideoCommmon = (oid: number | string, pn: number, sort: 0 | 1 | 2) =>
  request<commonList>({
    url: `/v2/reply`,
    params: {
      type: 1,
      oid,
      sort,
      pn,
    },
  })
